main: Fix UID check based on sysroot path
authorMatthew Barnes <mbarnes@redhat.com>
Tue, 14 Jul 2015 17:14:02 +0000 (13:14 -0400)
committerMatthew Barnes <mbarnes@redhat.com>
Thu, 16 Jul 2015 16:49:28 +0000 (12:49 -0400)
This is another place where we were hard-coding a default.  Create the
OstreeSysroot first and THEN check if its path is the root directory.

src/ostree/ot-main.c

index 7f45b2b46994bd4159567a3b02ef198c71f34d17..d3a7fd457ae7b0aca9e38d2f6feae8218b08eee4 100644 (file)
@@ -33,7 +33,7 @@
 #include "otutil.h"
 
 static char *opt_repo;
-static char *opt_sysroot = "/";
+static char *opt_sysroot;
 static gboolean opt_verbose;
 static gboolean opt_version;
 static gboolean opt_print_current_dir;
@@ -323,10 +323,17 @@ ostree_admin_option_context_parse (GOptionContext *context,
   if (!ostree_option_context_parse (context, main_entries, argc, argv, OSTREE_BUILTIN_FLAG_NO_REPO, NULL, cancellable, error))
     goto out;
 
+  if (opt_sysroot != NULL)
+    sysroot_path = g_file_new_for_path (opt_sysroot);
+
+  sysroot = ostree_sysroot_new (sysroot_path);
+
   if (flags & OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER)
     {
-      if ((opt_sysroot == NULL || strcmp (opt_sysroot, "/") == 0)
-          && getuid () != 0)
+      GFile *path = ostree_sysroot_get_path (sysroot);
+
+      /* If sysroot path is "/" then user must be root. */
+      if (!g_file_has_parent (path, NULL) && getuid () != 0)
         {
           g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
                        "You must be root to perform this command");
@@ -334,9 +341,6 @@ ostree_admin_option_context_parse (GOptionContext *context,
         }
     }
 
-  sysroot_path = g_file_new_for_path (opt_sysroot);
-  sysroot = ostree_sysroot_new (sysroot_path);
-
   if (opt_print_current_dir)
     {
       g_autoptr(GPtrArray) deployments = NULL;